package com.develop.wechatpay.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.develop.wechatpay.service.KingdeeK3CloudService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class KingdeeK3CloudServiceImpl implements KingdeeK3CloudService {

    @Value("${kingdee.k3cloud.acctID}")
    private String acctID;//账套Id


    @Value("${kingdee.k3cloud.userName}")
    private String userName;//用户名

    @Value("${kingdee.k3cloud.appId}")
    private String appId;//应用ID

    @Value("${kingdee.k3cloud.appSecret}")
    private String appSecret;//应用秘钥

    @Value("${kingdee.k3cloud.lcId}")
    private String lcId;//语言

    @Value("${kingdee.k3cloud.url}")
    private String url;//地址

    @Override
    public JSONObject loginByAppSecret(JSONArray array) throws Exception {
        log.info(array.toJSONString());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("parameters", array);
        String re = HttpUtil.post(url + "Kingdee.BOS.WebApi.ServicesStub.AuthService.LoginByAppSecret.common.kdsvc", jsonObject.toJSONString());
        return JSONObject.parseObject(re);
    }

    @Override
    public JSONObject loginByAppSecret() throws Exception {
        JSONArray array = new JSONArray();
        array.add(acctID);
        array.add(userName);
        array.add(appId);
        array.add(appSecret);
        array.add(lcId);
        return loginByAppSecret(array);
    }

    @Override
    public JSONObject sendPostRequest(String apiUrl, JSONArray array) throws Exception {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("parameters", array);
        Map<String, String> headers = new HashMap<>();
        String re = HttpUtil.createPost(url + apiUrl).addHeaders(headers).body(jsonObject.toJSONString()).execute().body();
        return JSONObject.parseObject(re);
    }

    @Override
    public JSONObject sendPostRequestAutoLogin(String apiUrl, JSONArray array) throws Exception {
        JSONObject loginByAppSecret = this.loginByAppSecret();
        String kdsvcSessionId = loginByAppSecret.getString("KDSVCSessionId");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("parameters", array);
        Map<String, String> headers = new HashMap<>();
        headers.put("Cookie", "kdservice-sessionid=" + kdsvcSessionId + "; path=/; HttpOnly");
        String re = HttpUtil.createPost(url + apiUrl).addHeaders(headers).body(jsonObject.toJSONString()).execute().body();
        return JSONObject.parseObject(re);
    }

    @Override
    public JSONObject sendPostRequestAutoLogin(String apiUrl, JSONObject oneJsonObject) throws Exception {
        JSONObject loginByAppSecret = this.loginByAppSecret();
        String kdsvcSessionId = loginByAppSecret.getString("KDSVCSessionId");
        JSONObject jsonObject = new JSONObject();
        JSONArray array = new JSONArray();
        array.add(oneJsonObject);
        jsonObject.put("parameters", array);
        Map<String, String> headers = new HashMap<>();
        headers.put("Cookie", "kdservice-sessionid=" + kdsvcSessionId + "; path=/; HttpOnly");
        String re = HttpUtil.createPost(url + apiUrl).addHeaders(headers).body(jsonObject.toJSONString()).execute().body();
        return JSONObject.parseObject(re);
    }

}
